package com.lagou.edu.ad.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.edu.ad.entity.PromotionAd;
import com.lagou.edu.ad.entity.PromotionSpace;
import com.lagou.edu.ad.service.IPromotionAdService;
import com.lagou.edu.ad.service.IPromotionSpaceService;
import com.lagou.edu.dto.PromotionAdDTO;
import com.lagou.edu.dto.PromotionSpaceDTO;
import com.lagou.edu.remote.AdRemoteService;
import com.lagou.edu.util.ConverUtil;
import com.lagou.edu.util.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ad/space")
public class AdRemoteServiceImpl implements AdRemoteService {

    @Autowired
    private IPromotionSpaceService promotionSpaceService;

    @Autowired
    private IPromotionAdService promotionAdService;

    @GetMapping("/list")
    public List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> list = promotionSpaceService.list();
        List<PromotionSpaceDTO> spaceDTOList = ConverUtil.convertList(list, PromotionSpaceDTO.class);
        return spaceDTOList;
    }

    // 根据 spacekey 查找广告位上的广告
    @GetMapping("/getAdBySpaceKey")
    public List<PromotionSpaceDTO> getAdBySpaceKey(String[] spaceKey) {
        List<PromotionSpaceDTO> spaceDTOList = new ArrayList<>();
        for (String key : spaceKey) {
            PromotionSpaceDTO dto = new PromotionSpaceDTO();

            QueryWrapper<PromotionSpace> wrapper = new QueryWrapper();
            wrapper.eq("spaceKey", key);
            PromotionSpace promotionSpace = promotionSpaceService.getOne(wrapper);

            QueryWrapper<PromotionAd> adQueryWrapper = new QueryWrapper<>();
            adQueryWrapper.eq("spaceId", promotionSpace.getId());
            //状态为上线状态
            adQueryWrapper.eq("status", 1);
            //有效期内
            Date now = new Date();
            adQueryWrapper.lt("startTime", now);
            adQueryWrapper.gt("endTime", now);
            List<PromotionAd> promotionAdList = promotionAdService.list(adQueryWrapper);

            ConverUtil.convert(promotionSpace, dto);
            dto.setPromotionAdDTOList(ConverUtil.convertList(promotionAdList, PromotionAdDTO.class));
            spaceDTOList.add(dto);
        }
        return spaceDTOList;
    }

    @PostMapping("/space/saveOrUpdate")
    public ResponseDTO saveOrUpdateSpace(@RequestBody PromotionSpaceDTO spaceDTO) {
        PromotionSpace entity = ConverUtil.convert(spaceDTO, PromotionSpace.class);

        if (entity.getId() == null) {
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            entity.setIsDel(0);
        } else {
            entity.setUpdateTime(new Date());
        }

        ResponseDTO responseDTO = null;
        try {
            promotionSpaceService.saveOrUpdate(entity);
            responseDTO = ResponseDTO.success();
        } catch (Exception e) {
            responseDTO = ResponseDTO.ofError(e.getMessage());
            e.printStackTrace();
        }
        return responseDTO;
    }

    @GetMapping("/space/getSpaceById")
    public PromotionSpaceDTO getSpaceById(@RequestParam("id") Integer id) {
        PromotionSpace promotionSpace = promotionSpaceService.getById(id);
        return ConverUtil.convert(promotionSpace,PromotionSpaceDTO.class);
    }

    @GetMapping("/getAllAds")
    public List<PromotionAdDTO> getAllAds() {
        List<PromotionAd> promotionAdList = promotionAdService.list();
        return ConverUtil.convertList(promotionAdList,PromotionAdDTO.class);
    }
}
